﻿// _msg_hook.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include "StdAfx.h"


using namespace std;

typedef void(*Msg_Callback)(int, USHORT, string);
typedef void(*SetProcess)(HWND);//注册目标程序的句柄
typedef void(*SendString)(ULONG, string);//发送字符串
typedef void(*RegistCallBack)(Msg_Callback);
typedef void(*RegistHook)();
typedef void(*RemoveHook)();

struct WNDINFO
{
	DWORD dwProcessId;
	HWND hWnd;
}; 

HWND GetProcessMainWnd(DWORD dwProcessId)
{
	WNDINFO wi;
	wi.dwProcessId = dwProcessId;
	wi.hWnd = NULL;
	EnumWindows([](HWND hWnd, LPARAM lParam)->BOOL {
		DWORD dwProcessId;
		GetWindowThreadProcessId(hWnd, &dwProcessId);
		WNDINFO& pInfo = *reinterpret_cast<WNDINFO*>(lParam);
		if (dwProcessId == pInfo.dwProcessId)
		{
			pInfo.hWnd = hWnd;
			return FALSE;
		}
		return TRUE;
	}, (LPARAM)&wi);
	return wi.hWnd;
}
HMODULE msgtrans;
RegistCallBack registcallback = nullptr;
SetProcess setprocess = nullptr;
SendString sendstring = nullptr;
queue <pair<HWND, pair<USHORT, string>>> msgqueue;

void OnTargetMessage(int code, USHORT key, string value) {
	printf("收到了消息 code:%d key:%d value:%s",code,key,value.c_str());
}

void MainLoop() {
	msgtrans = LoadLibraryA(".\\msgtrans.dll");
	registcallback = (RegistCallBack)GetProcAddress(msgtrans, "RegistCallBack");
	setprocess =(SetProcess)GetProcAddress(msgtrans, "SetProcess");
	sendstring = (SendString)GetProcAddress(msgtrans, "SendString");
	//registHook = (RegistHook)GetProcAddress(msgtrans, "RegistHook");
	if (!msgtrans || registcallback == nullptr)
	{
		std::cout << "msgtrans.dll!加载失败" << endl;
		system("pause");
		return;
	}
	registcallback(OnTargetMessage);
	MSG msg;
	while (true)//从系统的消息队列中取出一条消息
	{
		PeekMessage(&msg, NULL, NULL, NULL, NULL);
		
		if (msg.message == WM_COPYDATA)
		{
			printf("WM_COPYDATA:%d",msg.time);
			DispatchMessage(&msg);
		}

		if (!msgqueue.empty())
		{
			pair<HWND, pair<USHORT, string>>& pir0 = msgqueue.front();
			setprocess(pir0.first);
			sendstring(pir0.second.first, pir0.second.second);
			msgqueue.pop();
		}
		Sleep(1);
	}
	FreeLibrary(msgtrans);
}

void InputLoop() {
	DWORD processid = GetCurrentProcessId();
	HWND hWnd = GetProcessMainWnd(processid);
	if (hWnd == nullptr)
	{
		cout << "目标窗口找不到！" << endl;
		system("pause");
		return;
	}
	std::cout << "进程:" << processid << endl;
	std::cout << "句柄:" << hWnd << endl;
	while (true)
	{
		printf("请输入目标进程id:");
		DWORD targetid = 0;
		std::cin >> targetid;
		HWND thWnd = GetProcessMainWnd(targetid);
		if (thWnd == nullptr)
		{
			cout << "目标程序窗口句柄无效！" << endl;
			continue;
		}
		//HWND thWnd = hWnd;

		printf("请输入发送的消息:");
		char msg[60] = {};
		std::cin >> msg;

		msgqueue.push({ (HWND)thWnd ,{ 1, string(msg) } });
	}
}

int main()
{
	std::thread td(MainLoop);
	InputLoop();
	system("pause");
	return 1;
}

// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单

// 入门使用技巧: 
//   1. 使用解决方案资源管理器窗口添加/管理文件
//   2. 使用团队资源管理器窗口连接到源代码管理
//   3. 使用输出窗口查看生成输出和其他消息
//   4. 使用错误列表窗口查看错误
//   5. 转到“项目”>“添加新项”以创建新的代码文件，或转到“项目”>“添加现有项”以将现有代码文件添加到项目
//   6. 将来，若要再次打开此项目，请转到“文件”>“打开”>“项目”并选择 .sln 文件
